package m202405.d27;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/27 12:53
 */
public class Q131 {

    public static void main(String[] args) {
        Q131 q131 = new Q131();
        q131.partition("aba");
    }

    public List<List<String>> partition(String s) {
        List<List<String>> rs = new ArrayList<>();
        split(s.toCharArray(), 0, new LinkedList<>(), rs);
        return rs;
    }

    private void split(char[] chars, int start, LinkedList<String> list, List<List<String>> rs) {
        if (start == chars.length) {
            rs.add(new ArrayList<>(list));
            return;
        }
        for (int i = 1; i <= chars.length - start; i++) {
            boolean flag = isValid(chars, start, start + i - 1);
            if (flag) {
                list.addLast(String.valueOf(chars, start, i));
                split(chars, start + i, list, rs);
                list.removeLast();
            }
        }
    }

    private boolean isValid(char[] chars, int start, int end) {
        while (start <= end) {
            if (chars[start] != chars[end]) {
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
}
